package com.cjf.aop;

import android.app.Application;
import android.widget.Toast;

import com.cjf.aop.runtime.AOPManager;
import com.cjf.aop.runtime.checker.IThrowableHandler;
import com.cjf.aop.runtime.checker.Interceptor;
import com.cjf.aop.runtime.logger.XLogger;
import com.cjf.aop.runtime.utils.PermissionUtils;
import com.cjf.aop.runtime.utils.Utils;

import org.aspectj.lang.ProceedingJoinPoint;

import java.util.List;

/**
 * created by cjf
 * on: 2020/3/26
 */
public class MyApplication extends Application {
    public static final int INTERCEPT_LOGIN = 10;
    public static final String TRY_CATCH_KEY = "getNumber";

    @Override
    public void onCreate() {
        super.onCreate();
        AOPManager.init(this);
        AOPManager.debug(true);
        AOPManager.setInterceptor(new Interceptor() {
            @Override
            public boolean intercept(int type, ProceedingJoinPoint joinPoint) throws Throwable {
                XLogger.d("正在进行拦截，拦截类型:" + type);
                switch (type) {
                    case 1:    //做你想要的拦截
                        return false;
                    case 2:
                        return true; //return true，直接拦截切片的执行
                    case INTERCEPT_LOGIN:
                        if (true) {//没登录,进行拦截,跳转到登录界面
                            Toast.makeText(AOPManager.getContext(), "请先进行登陆", Toast.LENGTH_SHORT).show();
                            return true;
                        }
                    default:
                        break;
                }
                return false;
            }
        });
        AOPManager.setThrowableHandler(new IThrowableHandler() {
            @Override
            public Object handleThrowable(String flag, Throwable throwable) {
                XLogger.d("捕获到异常，异常的flag:" + flag);
                switch (flag) {
                    case TRY_CATCH_KEY:
                        return 100;
                }
                return null;
            }
        });
        AOPManager.setOnPermissionDeniedListener(new PermissionUtils.OnPermissionDeniedListener() {
            @Override
            public void onDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied, List<String> permissionsGranted) {
                XLogger.e("申请权限拒绝情况: \n 拒绝列表:{ " + Utils.listToString(permissionsDenied)
                        + "} \n 拒绝并不在询问列表:{" + Utils.listToString(permissionsDeniedForever)
                        + "} \n 同意授权列表:{" + Utils.listToString(permissionsGranted) + "}");
            }
        });
    }
}
